/*
 * InputNode.java
 * This file is part of the <a href="http://code.google.com/p/pux">PUX Framework</a>
 *
 * Copyright 2008 by <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package de.teamskill.pux.library.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * InputNode
 * 
 * @author <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 * 
 */
public class InputNode extends WizardNode {

	/**
	 * question
	 */
	private String question;

	/**
	 * inputField
	 */
	private JTextField inputField;

	/**
	 * InputNode Constructor
	 * 
	 */
	public InputNode() {
		this("INPUT" + nr++, false, "Your Input:");
	}

	/**
	 * InputNode Constructor
	 * 
	 * @param nodeName
	 * @param continuous
	 */
	public InputNode(String nodeName, boolean continuous, String question) {
		super(nodeName, continuous);
		this.question = question;
	}

	/**
	 * @see de.teamskill.pux.library.gui.WizardNode#actCancel(java.awt.event.ActionEvent)
	 */
	@Override
	protected void actCancel(ActionEvent e) {
		getFrame().setVisible(false);
	}

	/**
	 * @see de.teamskill.pux.library.gui.WizardNode#actOK(java.awt.event.ActionEvent)
	 */
	@Override
	protected void actOK(ActionEvent e) {
		String text = inputField.getText();
		getOutputData().pushData(text);
		getFrame().setVisible(false);
	}

	/**
	 * @see de.teamskill.pux.library.gui.WizardNode#createContentPanel()
	 */
	@Override
	protected Component createContentPanel() {
		JPanel p = new JPanel(new BorderLayout());
		p.add(makeInputLabel(), BorderLayout.NORTH);
		inputField = makeInputField();
		p.add(inputField, BorderLayout.CENTER);
		return p;
	}

	/**
	 * makeInputField
	 * 
	 * @return
	 */
	private JTextField makeInputField() {
		JTextField f = new JTextField();
		f.setEditable(true);
		f.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actOK(e);
			}
		});
		return f;
	}

	/**
	 * makeInputLabel
	 * 
	 * @return
	 */
	private Component makeInputLabel() {
		JLabel c = new JLabel(question, JLabel.LEADING);
		return c;
	}

}
